home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / space / imlib.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  8KB  |  276 lines

  1. /*
  2.  * Copyright (C) 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/gl.h>
  18. #include <gl/image.h>
  19. #include <GL/glx.h>
  20. #include <GL/gl.h>
  21.  
  22. typedef short            sint16 ;
  23. typedef unsigned short   uint16 ;
  24. typedef long             sint32 ;
  25. typedef unsigned long    uint32 ;
  26. typedef float            flot32 ;
  27. typedef double           flot64 ;
  28.  
  29. void   savescreen(uint32,uint32) ;
  30. sint32 imgtolrect(char *) ;
  31. void   lrecttoimg(char *,sint32,sint32,sint32,sint32,uint32 *) ;
  32.  
  33. static void sizeofimage(char *name, sint32 *xsize, sint32 *ysize) ;
  34. static void bwtocpack(uint16 *b,uint32 *l,sint32 n) ;
  35. static void rgbtocpack(uint16 *r,uint16 *g,uint16 *b,uint32 *l,sint32 n) ;
  36. static void rgbatocpack(uint16 *r,uint16 *g,uint16 *b,uint16 *a,uint32 *l,sint32 n) ;
  37. static void cpacktorgba(uint32 *l,uint16 *r,uint16 *g,uint16 *b,uint16 *a,sint32 n) ;
  38.  
  39. /********************************************************************** 
  40. *  savescreen()  -
  41. **********************************************************************/
  42. void savescreen(uint32 dx,uint32 dy)
  43.  
  44. {  uint32 *ptr ;
  45.  
  46.    if ((ptr = (uint32 *) malloc(4*dx*dy)) == 0) {
  47.      printf("screendump malloc failed\n") ;
  48.      exit(0) ;
  49.      }
  50.  
  51.    glViewport(0,0,dx,dy) ;
  52.  
  53.    glReadPixels(0,0,dx,dy,GL_RGBA,GL_UNSIGNED_BYTE,ptr);
  54.  
  55.    lrecttoimg("image.rgb",0,0,dx-1,dy-1,ptr) ;
  56.  
  57.    free(ptr) ;
  58. }
  59.  
  60. /***********************************************************************
  61. *  sizeofimage()  -
  62. **********************************************************************/
  63. static void sizeofimage(char *name, long *xsize, long *ysize)
  64. {
  65.     IMAGE *image;
  66.  
  67.     image = iopen(name,"r");
  68.     if(!image) {
  69.         fprintf(stderr,"sizeofimage: can't open image file %s\n",name);
  70.         exit(1);
  71.     }
  72.     *xsize = image->xsize;
  73.     *ysize = image->ysize;
  74.     iclose(image);
  75. }
  76.  
  77. /********************************************************************** 
  78. *  imgtolrect()  -
  79. **********************************************************************/
  80. sint32 imgtolrect(char *name)
  81. {
  82.     uint32 *base, *lptr;
  83.     uint16 *rbuf, *gbuf, *bbuf, *abuf;
  84.     IMAGE  *image;
  85.     sint32 y;
  86.  
  87.     image = iopen(name,"r");
  88.     if(!image) {
  89.     fprintf(stderr,"longimagedata: can't open image file %s\n",name);
  90.     exit(1);
  91.     }
  92.     base = (unsigned long *)malloc(image->xsize*image->ysize*sizeof(unsigned long));
  93.     rbuf = (uint16 *)malloc(image->xsize*sizeof(short));
  94.     gbuf = (uint16 *)malloc(image->xsize*sizeof(short));
  95.     bbuf = (uint16 *)malloc(image->xsize*sizeof(short));
  96.     abuf = (uint16 *)malloc(image->xsize*sizeof(short));
  97.     if(!base || !rbuf || !gbuf || !bbuf) {
  98.     fprintf(stderr,"longimagedata: can't malloc enough memory\n");
  99.     exit(1);
  100.     }
  101.     lptr = base;
  102.     for(y=0; y<image->ysize; y++) {
  103.     if(image->zsize>=4) {
  104.         getrow(image,rbuf,y,0);
  105.         getrow(image,gbuf,y,1);
  106.         getrow(image,bbuf,y,2);
  107.         getrow(image,abuf,y,3);
  108.         rgbatocpack(rbuf,gbuf,bbuf,abuf,lptr,image->xsize);
  109.         lptr += image->xsize;
  110.     } else if(image->zsize==3) {
  111.         getrow(image,rbuf,y,0);
  112.         getrow(image,gbuf,y,1);
  113.         getrow(image,bbuf,y,2);
  114.         rgbtocpack(rbuf,gbuf,bbuf,lptr,image->xsize);
  115.         lptr += image->xsize;
  116.     } else {
  117.         getrow(image,rbuf,y,0);
  118.         bwtocpack(rbuf,lptr,image->xsize);
  119.         lptr += image->xsize;
  120.     }
  121.     }
  122.     iclose(image);
  123.     free(rbuf);
  124.     free(gbuf);
  125.     free(bbuf);
  126.     free(abuf);
  127.     return((int)base);
  128. }
  129.  
  130. /********************************************************************** 
  131. *  bwtocpack()  -
  132. **********************************************************************/
  133. static void bwtocpack(uint16 *b,uint32 *l,sint32 n)
  134. {
  135.     while(n>=8) {
  136.     l[0] = 0x00010101*b[0];
  137.     l[1] = 0x00010101*b[1];
  138.     l[2] = 0x00010101*b[2];
  139.     l[3] = 0x00010101*b[3];
  140.     l[4] = 0x00010101*b[4];
  141.     l[5] = 0x00010101*b[5];
  142.     l[6] = 0x00010101*b[6];
  143.     l[7] = 0x00010101*b[7];
  144.     l += 8;
  145.     b += 8;
  146.     n -= 8;
  147.     }
  148.     while(n--) 
  149.     *l++ = 0x00010101*(*b++);
  150. }
  151.  
  152. /********************************************************************** 
  153. *  rgbtocpack()  -
  154. **********************************************************************/
  155. static void rgbtocpack(uint16 *r,uint16 *g,uint16 *b,uint32 *l,sint32 n)
  156. {
  157.     while(n>=8) {
  158.     l[0] = r[0] | (g[0]<<8) | (b[0]<<16);
  159.     l[1] = r[1] | (g[1]<<8) | (b[1]<<16);
  160.     l[2] = r[2] | (g[2]<<8) | (b[2]<<16);
  161.     l[3] = r[3] | (g[3]<<8) | (b[3]<<16);
  162.     l[4] = r[4] | (g[4]<<8) | (b[4]<<16);
  163.     l[5] = r[5] | (g[5]<<8) | (b[5]<<16);
  164.     l[6] = r[6] | (g[6]<<8) | (b[6]<<16);
  165.     l[7] = r[7] | (g[7]<<8) | (b[7]<<16);
  166.     l += 8;
  167.     r += 8;
  168.     g += 8;
  169.     b += 8;
  170.     n -= 8;
  171.     }
  172.     while(n--) 
  173.         *l++ = *r++ | ((*g++)<<8) | ((*b++)<<16);
  174. }
  175.  
  176. /********************************************************************** 
  177. *  rgbatocpack()  -
  178. **********************************************************************/
  179. static void rgbatocpack(uint16 *r,uint16 *g,uint16 *b,uint16 *a,uint32 *l,sint32 n)
  180. {
  181.     while(n>=8) {
  182.     l[0] = r[0] | (g[0]<<8) | (b[0]<<16) | (a[0]<<24);
  183.     l[1] = r[1] | (g[1]<<8) | (b[1]<<16) | (a[1]<<24);
  184.     l[2] = r[2] | (g[2]<<8) | (b[2]<<16) | (a[2]<<24);
  185.     l[3] = r[3] | (g[3]<<8) | (b[3]<<16) | (a[3]<<24);
  186.     l[4] = r[4] | (g[4]<<8) | (b[4]<<16) | (a[4]<<24);
  187.     l[5] = r[5] | (g[5]<<8) | (b[5]<<16) | (a[5]<<24);
  188.     l[6] = r[6] | (g[6]<<8) | (b[6]<<16) | (a[6]<<24);
  189.     l[7] = r[7] | (g[7]<<8) | (b[7]<<16) | (a[7]<<24);
  190.     l += 8;
  191.     r += 8;
  192.     g += 8;
  193.     b += 8;
  194.     a += 8;
  195.     n -= 8;
  196.     }
  197.     while(n--) 
  198.         *l++ = *r++ | ((*g++)<<8) | ((*b++)<<16) | ((*a++)<<24);
  199. }
  200.  
  201. /********************************************************************** 
  202. *  lrecttoimg()  -
  203. **********************************************************************/
  204. void lrecttoimg(char *name,sint32 x1,sint32 y1,sint32 x2,sint32 y2,uint32 *lbuf)
  205.  
  206. {   sint32 xsize, ysize, y;
  207.     IMAGE  *oimage;
  208.     uint16 *rbuf, *gbuf, *bbuf, *abuf;
  209.  
  210.     xsize = x2-x1+1;
  211.     ysize = y2-y1+1;
  212.     rbuf = (uint16 *)malloc(xsize*sizeof(short));
  213.     gbuf = (uint16 *)malloc(xsize*sizeof(short));
  214.     bbuf = (uint16 *)malloc(xsize*sizeof(short));
  215.     abuf = (uint16 *)malloc(xsize*sizeof(short));
  216.     oimage = iopen(name,"w",RLE(1),3,xsize,ysize,4);
  217.     if(!oimage) {
  218.     fprintf(stderr,"lrecttoimage: can't open output file\n");
  219.     exit(1);
  220.     }
  221.     for(y=0; y<ysize; y++) {
  222.     cpacktorgba(lbuf,rbuf,gbuf,bbuf,abuf,xsize);
  223.     putrow(oimage,rbuf,y,0);
  224.     putrow(oimage,gbuf,y,1);
  225.     putrow(oimage,bbuf,y,2);
  226.     putrow(oimage,abuf,y,3);
  227.     lbuf += xsize;
  228.     }
  229.     iclose(oimage);
  230.     free(lbuf);
  231.     free(rbuf);
  232.     free(gbuf);
  233.     free(bbuf);
  234. }
  235.  
  236. #define CPACKTORGBA(l,r,g,b,a)            \
  237.     val = (l);                \
  238.     (r) = (val>>0) & 0xff;            \
  239.     (g) = (val>>8) & 0xff;            \
  240.     (b) = (val>>16) & 0xff;            \
  241.     (a) = (val>>24) & 0xff;            
  242.  
  243. /********************************************************************** 
  244. *  cpacktorgba()  -
  245. **********************************************************************/
  246. static void cpacktorgba(uint32 *l,uint16 *r,uint16 *g,uint16 *b,uint16 *a,sint32 n)
  247. {
  248.     unsigned long val;
  249.  
  250.     while(n>=8) {
  251.     CPACKTORGBA(l[0],r[0],g[0],b[0],a[0]);
  252.     CPACKTORGBA(l[1],r[1],g[1],b[1],a[1]);
  253.     CPACKTORGBA(l[2],r[2],g[2],b[2],a[2]);
  254.     CPACKTORGBA(l[3],r[3],g[3],b[3],a[3]);
  255.     CPACKTORGBA(l[4],r[4],g[4],b[4],a[4]);
  256.     CPACKTORGBA(l[5],r[5],g[5],b[5],a[5]);
  257.     CPACKTORGBA(l[6],r[6],g[6],b[6],a[6]);
  258.     CPACKTORGBA(l[7],r[7],g[7],b[7],a[7]);
  259.     l += 8;
  260.     r += 8;
  261.     g += 8;
  262.     b += 8;
  263.     a += 8;
  264.     n -= 8;
  265.     }
  266.     while(n--) {
  267.     CPACKTORGBA(l[0],r[0],g[0],b[0],a[0]);
  268.     l++;
  269.     r++;
  270.     g++;
  271.     b++;
  272.     a++;
  273.     }
  274. }
  275.  
  276.